package com.woniuxy.stage3demo.controller.controllerhpd;


import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.hpd.ProductBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.servicehpd.IProductLogService;
import com.woniuxy.stage3demo.service.servicehpd.IProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/product")
@RestController
public class ProductController {
    @Resource
    private IProductService productServiceImpl;
    @Resource
    private IProductLogService productLogServiceImpl;

    //分页查询
    @RequestMapping(value = "/page",produces = {"application/json;charset=utf-8"})
    public TableResult<ProductBean> findAllByParams(PageRequest request, ProductBean productBean){
        PageInfo<ProductBean> pageInfo = productServiceImpl.findAllByParams(request, productBean);
        return new TableResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    //更改单个产品状态
    @PutMapping(value = "/status/{id}/{status}",produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        int opt;
        if(id != null && status != null) {
            ProductBean productBean=new ProductBean();
            productBean.setId(id);
            productBean.setProductStatus(status);
            productServiceImpl.updateStatus(productBean);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }

    //批量更改产品状态
    @PutMapping(value = "/mulOpt/{ids}/{selOpt}",produces = {"application/json;charset=utf-8"})
    public ResultMsg mulStatusUpdate(@PathVariable("ids") Long ids[],@PathVariable("selOpt") int selOpt){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        for (Long id:ids) {
            productServiceImpl.batchOpt(selOpt,id);
        }
        return res;
    }


    //删除产品
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg productDel(@PathVariable("id") Long id){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null) {
            productServiceImpl.deleteProduct(id);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }

    //修改产品信息
    @PutMapping(value = "/info",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(@RequestBody ProductBean productBean){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        ProductBean oldProductBean = productServiceImpl.findOneById(productBean.getId());
        if(productServiceImpl.updateProduct(productBean)==1){
            productLogServiceImpl.addProductLog(oldProductBean,productBean);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

    //新增产品
    @PostMapping(value = "/add",produces = {"application/json;charset=utf-8"})
    public ResultMsg addProduct(@RequestBody ProductBean productBean){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(productServiceImpl.addProduct(productBean) == 1){
            ProductBean oldProductBean=new ProductBean();
            productLogServiceImpl.addProductLog(oldProductBean,productBean);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

}
